package com.itheima.service.db.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.AnnouncementMapper;
import com.itheima.service.db.AnnouncementService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/22 19:05
 * @Version 1.0
 */

@DubboService
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;


    @Override
    public PageBeanVo findAnnouncementByPage(Integer pageNum, Integer pageSize) {

        //1.开启分行
        Page<Announcement> page = new Page<>(pageNum, pageSize);
        //2.构建条件
        LambdaQueryWrapper<Announcement> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Announcement::getCreated);
        //3.查询

        page = announcementMapper.selectPage(page, wrapper);

        //4.封装pageBeanVo
        return new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());


    }

    @Override
    public void save(String title, String description) {

        Announcement announcement = new Announcement();

        announcement.setTitle(title);
        announcement.setDescription(description);
        announcement.setCreated(new Date(System.currentTimeMillis()));
        announcement.setUpdated(new Date(System.currentTimeMillis()));

        announcementMapper.insert(announcement);

    }


}
